<?php
class TopListController  extends Controller {

	public function __construct($data) {
        parent::__construct($data);
    }
    
    public function index() {
    	 require_once( '../system/ecjp_m.php' );
    	 $this->_load_php_file("chklogin.php");
         if(!isset($_SESSION['USER'])){	    	
	    require_once( '../system/cookie.php' );  
	    }
	    
	      $this->_load_php_file("Class_FUNC.php");
	      $this->_load_php_file("Class_M_DAO.php");
	      $db = new Class_M_DAO;
    	  $this->_load_mdao_class("t_m_goods");
	      $goods_dao = new t_m_goods;
    	 
	       if ( isset($_SESSION['USER']) ){
	       	   $user = $_SESSION['USER'];
	       	   $login_id = $user['USER_id'];
	       	   $db = new Class_M_DAO;
    		   $sql = sprintf("SELECT * FROM D_HOPE WHERE login_id = '%s' AND del<>'*' ",
			   mysql_real_escape_string( $login_id ));
    		   $data = $db->get_rows( $sql );
	       	   
	       	    if (isset($user))
	            $this->assign('user', $user);
	       }
	   
	         $site = Class_FUNC::get_site();
	    if(isset($site['site_keyword']))
	    $this->assign('site_keyword', $site['site_keyword']);
        if(isset($site['site_description']))
	    $this->assign('site_description', $site['site_description']);
	    if(isset($site['site_title']))
	    $this->assign('site_title','カテゴリ | '.$site['site_title']);
	      	      	      
    	 //売上ランキング
	     $sql = "SELECT b.id,b.goods_name,b.goods_image,b.goods_unitprice FROM d_good_rank a inner JOIN d_goods b ON a.good_id = b.id ORDER BY a.rank";
	     $charts_data = $goods_dao->get_rows($sql);
	      foreach ($charts_data as &$value){
	          if(isset($value['goods_image']) && !empty($value['goods_image'])){
	     	     $value['goods_image'] = Class_FUNC::get_imagepath($value['id'],$value['goods_image'],101);
	       }
	       if(isset($value['goods_unitprice']) && !empty($value['goods_unitprice'])){
	          	 $value['goods_rate'] = $value['goods_unitprice']+$value['goods_unitprice'] * RATE;
	          }
	     }
	     if (isset($charts_data))
	     $this->assign('charts_data',$charts_data);
	     
	     //カテゴリで商品
       //  $wherearr = array();
       //  $wherearr[] = sprintf("(a.goods_mark1= '%s' and a.goods_status= '%s'  )",'1','1');
	   //  $goods_data = $goods_dao->search_csv($wherearr);
	   //  if (isset($goods_data))
	   //  $this->assign('goods_data', $goods_data);
	     
	    $forms = $this->g; 
        $wherearr = array();
        if (isset($forms['category_id']) && (!empty($forms['category_id']) )) {
        	$keyword = $forms['category_id'];
        	$this->assign('category_id', $forms['category_id']);
        	
        	 $sql = 'SELECT * FROM  m_categorygood';         
        	 $category_data = $db->get_rows($sql);
        	 $arr = $this->build_tree($keyword,$category_data);
        	 
        	 $category_id[]=$keyword;
        	 foreach ($arr as $k => $v){
        	 	$category_id[]=$v['category_cd'];
        	 }
        	 $category_id = implode(",",$category_id);
        	 print_r($category_id);
        	 
            $wherearr[] = sprintf("(a.id in (select goods_id from m_goods_category where category_id in(%s)) )", $category_id);
             
             
            
              $this->_load_php_file("Class_FUNC.php");
	          $goodsset = Class_FUNC::get_goodsset();
	           if(isset($goodsset[$forms['category_id']]))
	          $set = $goodsset[$forms['category_id']];
             
        }
        
      
	     
	   if (isset($forms['page']) && is_numeric($forms['page'])) {
            $dosearch = TRUE;
            $page['current'] = $forms['page'];
        } else {
            $page['current'] = 1;
        }
       

       if(isset($set)){
        	if(isset($set['listnumber']) && !empty($set['listnumber'])){
        		 $page['item'] = $set['listnumber'];
        	}else {
        		 $page['item'] =  PAGE_MAX;        		 
        	}
        	 $this->assign('set', $set);
        }else {
        	 $page['item'] =  PAGE_MAX;
        }
        
        
        $goods_data = $goods_dao->search($wherearr, $page);
	     
 
        if(isset($data) && !empty($data)){
        	   foreach ($data as $value) {
                 if(isset($value['goods_id']) && (!empty($value['goods_id']))){
    				 $id[] = $value['goods_id']; 				 
    			}           
                  
              }
                
           foreach( $goods_data as $kcol => $chkval ) {          	   
			 if(in_array($goods_data[$kcol]['id'],$id)){
			 	$goods_data[$kcol]['Y'] = 'Y';
			 }
			 
           }
        }
        
       foreach ($goods_data as &$value){
	          if(isset($value['goods_image']) && !empty($value['goods_image'])){
	     	     $value['goods_image'] = Class_FUNC::get_imagepath($value['id'],$value['goods_image'],101);
	       }
        if(isset($value['goods_unitprice']) && !empty($value['goods_unitprice'])){
	          	 $value['goods_rate'] = $value['goods_unitprice']+$value['goods_unitprice'] * RATE;
	          }
	     }
        
        
          //商品category
	       $sql = 'SELECT * FROM  m_categorygood';
           $category_data = $goods_dao->get_rows($sql);
           if (isset($category_data))
           $this->assign('category_data', $category_data);
         //What's New
             $this->_load_mdao_class("t_m_news");
	        $news_dao = new t_m_news;
           $sql = 'SELECT * FROM  m_news where site_id = 1 and  st_time<current_timestamp and ed_time>current_timestamp order by date desc limit 0,10';
           $newslist_data = $news_dao->get_rows($sql);
           if (isset($newslist_data))
           $this->assign('news_datalist', $newslist_data);
        
        
        if(isset($forms))
        $this->assign('forms', $forms);
        
        if (isset($goods_data))
	    $this->assign('goods_data', $goods_data);
	     
	    if (isset($page))
        $this->assign('page', $page);
	     
        $this->assign('sp_meta', $this->fetch('sp/sp_meta.html'));
	     $this->assign('pageing', $this->fetch('sp/toppage.html'));
	     $this->assign('chartsing', $this->fetch('sp/charts.html'));
	     $this->assign('categorying', $this->fetch('sp/categorying.html'));
	      $this->assign('newsing', $this->fetch('sp/news.html'));
	     $this->assign('sp_head', $this->fetch('sp/sp_head.html'));
	     $this->assign('sp_foot', $this->fetch('sp/sp_foot.html'));
    	 $this->display('sp/list.html');
    }
    
    public  function add(){
    	     require_once( '../system/ecjp_m.php' );
     	         $this->_load_php_file("Class_DB.php");
     	         $p=$this->p;
     	         
     	         $good_id = $p['goods_id'];
     	         $login_id = $p['login_id'];
     	         $sql = sprintf("INSERT INTO D_HOPE(login_id,goods_id) VALUES ('%s','%s') ",
			     mysql_real_escape_string( $login_id ),
			     mysql_real_escape_string( $good_id ));
     	         
     	         $db = new Class_DB;
     	         $db->query( $sql );   
    	
    }
    
    
    public function findChild(&$arr,$id){  
        $childs=array();  
        foreach ($arr as $k => $v){
           if($v['parent_category_cd']== $id){  
             $childs[]=$v;  
         }  
     }
        return $childs;
   } 

 public  function build_tree($root_id,$rows){
    // global $rows;  
     $childs=$this->findChild($rows,$root_id);  

     if(empty($childs)){ 
         return null;  
     }  

    foreach ($childs as $k => $v){  
        $rescurTree=$this->build_tree($v['category_cd'],$rows);  
        if( null != $rescurTree){   
        $childs[$k]['childs']=$rescurTree;  
        }  
    }  
     return $childs;  

 }  
    
    
}

?>